#include "threadPool.h"

void get_salt(char *salt, char* passwd)
{
    int i = 0;
    int position = 0;
    while(passwd[i])
    {
        if(passwd[i] == '$')
        {
            position = i;
        }
        i++;
       //printf("%c", passwd[i]);
    }
    //printf("\n");

    strncpy(salt, passwd, position + 1);
    //printf("salt OK\n");
}

//username: 用户名  passwd:用户密码
int confirmPasswd(char* username, char * passwd)
{
    struct spwd *sp;
     
    char salt[512] = {0};

    //printf("%s\n", username);
    //printf("%s\n", passwd);
    //通过用户名得到密文
    sp = getspnam(username);
    //printf("---------\n");
    //printf(" 密文为：%s\n", sp->sp_pwdp);
    
    //printf("密码为：%s\n", passwd);
    get_salt(salt, sp->sp_pwdp);
    //printf("盐值为：%s\n", salt);
    //printf("crypt = %s\n",crypt(passwd,salt));
    //printf("crypt2 = %s\n",crypt(passwd, "$y$j9T"));
    if(strcmp(sp->sp_pwdp, crypt(passwd, salt)) == 0)
    {
        printf("验证通过！\n");
        return 1;
    }
    else
    {
        printf("验证不通过！\n");
        return 0;
    }

}

